package com.网络编程.net.http.res;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author liyiruo
 * @Description
 * @Date 2021/12/4 上午10:44
 */
@Controller
public class ResPDF {
    /**
     * http请求时，这个方法可以返回pdf文件
     * @param response
     * @throws IOException
     */
   // @RequestMapping("/")
    public void getPdf(HttpServletResponse response) throws IOException {
        System.out.println("==========login2==========");
        //InputStream inputStream=null;
        response.setContentType("application/pdf");
        //服务器PDF所在位置,无所谓的，你爱放哪儿放哪儿，这边我是在项目里，用的相对路径
        //String realPath=request.getRealPath(PDF_PATH);
        String realPath = "/Users/liyiruo/Desktop/1.pdf";
        InputStream in = new FileInputStream(realPath);
        OutputStream out = response.getOutputStream();
        int b = 0;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        out.close();
        in.close();
        out.flush();
    }
}
